package com.ssyt.store.sys.dao.impl;

import org.springframework.stereotype.Repository;

import com.ssyt.store.common.BaseDao;
import com.ssyt.store.common.PageModel;
import com.ssyt.store.common.util.StringUtil;
import com.ssyt.store.sys.dao.StoreProductsDao;
import com.ssyt.store.sys.entity.StoreProduct;

@Repository
public class StoreProductsDaoImpl extends BaseDao<StoreProduct, String> implements StoreProductsDao{
	
	
	@Override
	public PageModel<StoreProduct> showAllProducts(int pageNo,int pageSize){
		//String hql = "SELECT new StoreProduct(p.id,p.pic,p.bannerPic,p.flag,p.name,p.description,p.price,p.state,c) FROM StoreProduct p,StoreCart c WHERE p.id = c.storeProductId" ;
		String hql = "FROM StoreProduct";
		return pageMultiPropertyByHqlUpper(pageNo,pageSize,hql);
	}

	@Override
	public StoreProduct productInfo(String productId) {
		StoreProduct product = get(productId);
		return product;
	}

	@Override
	public StoreProduct getProduct(String id) {
		StoreProduct product = get(id);
		return product;
	}

	@Override
	public PageModel<StoreProduct> ListAllProducts(int pageNo, int pageSize, String name, Integer flag, Integer price) {
		StringBuffer hql = new StringBuffer();
		hql.append("FROM StoreProduct WHERE 1=1");
		if(StringUtil.isNotEmpty(name)){
			hql.append(" AND name like '%" + name +"%'");
		}
		if(flag != 0 && flag != null){
			hql.append(" AND flag =" + flag);
		}
		if(price != 0 && price != null){
			
			if(price == 1){
				hql.append(" AND price<100");
			}else if(price == 2){
				hql.append(" AND 100<=price AND price<200");
			}else if(price == 3){
				hql.append(" AND 200<=price AND price<300");
			}else if(price == 4){
				hql.append(" AND 300<=price");
			}
			
		}
		return pageMultiPropertyByHqlUpper(pageNo,pageSize,hql.toString());
	}

	
}
